﻿using AnesSystem.Models;
using AnesSystem.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{

    /// <summary>
    /// PaiTai_Left.xaml 的交互逻辑
    /// </summary>
    public partial class NoSecheduledListCtrl : UserControl
    {

        #region Variables

        IList checkedList = new List<PatientLinkModel>();

        #endregion

        #region Constructor
        public NoSecheduledListCtrl()
        {
            InitializeComponent();
            this.DataContext = NoSecheduledListCtrlViewModel.Instance;
        }

        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void PaiBan(IList list)
        {
            DataObject dataObject = null;
            if (list.Count != 0)
            {
                dataObject = new DataObject("1", list);
            }
            else if (_dgNoSchedule.SelectedItem != null || _dgNoSchedule.SelectedValue != null)
            {
                dataObject = new DataObject("1", new List<PatientLinkModel>() { _dgNoSchedule.SelectedItems[0] as PatientLinkModel });
            }
            if (dataObject == null) return;
            System.Windows.DragDrop.DoDragDrop(_dgNoSchedule, dataObject, DragDropEffects.Copy);
        }
        private void PaiBan(PatientLinkModel item)
        {
            if (item == null)
            {
                MessageBoxCommon.ShowDialog("请先选择病人!", MessageBoxCommon.OK);
                return;
            }

            if (SecheduleMainCtrlViewModel.Instance.CurrentRoom == null)
            {
                MessageBoxCommon.ShowDialog("请选择手术间!", MessageBoxCommon.OK);
                return;
            }
            else
            {
                SecheduleMainCtrlViewModel.Instance.AddSecheduled(item);
                SecheduleMainCtrlViewModel.Instance.RefUI();
            }
        }

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        private void _dgNoSchedule_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                PatientLinkModel patientLinkModel = _dgNoSchedule.SelectedItem as PatientLinkModel;
                PaiBan(patientLinkModel);

            }
            catch (Exception ex)
            {
                Common.LogHelper.Error("NoSecheduledListCtrl", ex);
            }

        }

        private void _dgNoSchedule_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                checkedList.Clear();
                List<CheckBox> list = Common.UIHelper.FindVisualChild<CheckBox>(_dgNoSchedule);
                foreach (var item in list)
                {
                    if ((bool)item.IsChecked)
                    {
                        checkedList.Add(item.DataContext as PatientLinkModel);
                    }
                }
                PaiBan(checkedList);
            }
        }

        private void _dgNoSchedule_Drop(object sender, DragEventArgs e)
        {
            if (e.Data != null && e.Data.GetData("2") != null)
            {
                IList list = e.Data.GetData("2") as IList;
                SecheduleMainCtrlViewModel.Instance.DeleteSecheduled(list);
                SecheduleMainCtrlViewModel.Instance.RefUI();
            }
        }

        private void _dgNoSchedule_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data != null && e.Data.GetData("2") != null)
            {
                e.Effects = DragDropEffects.Copy;
                e.Handled = true;
            }
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {

        }

        #endregion


    }
}
